﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FksAutoTask
{
    public partial class frm_param : App_Code.WinClient
    {
        public frm_param()
        {
            InitializeComponent();
        }

        private void frm_param_Load(object sender, EventArgs e)
        {
            Init();

            Fks.DataBase.SqlObject sqlobject = new Fks.DataBase.SqlObject();
            string SQL_Select = "select * from param";
            DataSet myds = sqlobject.GetDataSet(SQL_Select);

            if (myds != null && myds.Tables.Count > 0 && myds.Tables[0] != null && myds.Tables[0].Rows.Count > 0)
            {
                this.cb_split.Text = myds.Tables[0].Rows[0]["split"].ToString();
                this.cb_service_type.Text = myds.Tables[0].Rows[0]["server_type"].ToString();
                this.tb_server.Text = myds.Tables[0].Rows[0]["email_server"].ToString();
                this.tb_user.Text = myds.Tables[0].Rows[0]["email_user"].ToString();
                this.tb_pass.Text = myds.Tables[0].Rows[0]["email_pwd"].ToString();
                this.tb_email.Text = myds.Tables[0].Rows[0]["send_email"].ToString();
                this.tb_cc_email.Text = myds.Tables[0].Rows[0]["cc_email"].ToString();
                this.tb_email_body.Text = myds.Tables[0].Rows[0]["email_body"].ToString();
                this.tb_email_title.Text = myds.Tables[0].Rows[0]["email_title"].ToString();
                this.che_Enable_SSL.Checked = Convert.ToString(myds.Tables[0].Rows[0]["enable_ssl"]) == "1" ? true : false;
            }
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            #region 输入校验

            bool check_input = true;
            string check_msg = string.Empty;
            if (this.cb_split.Text.Equals(""))
            {
                check_input = false;
                check_msg = "发送间隔不能为空！";
                this.cb_split.Focus();
            }

            if (this.cb_service_type.Text.Equals(""))
            {
                check_input = false;
                check_msg = "请选择服务器类型！";
                this.cb_service_type.Focus();
            }

            if (this.tb_server.Text.Equals(""))
            {
                check_input = false;
                check_msg = "发件服务器地址不能为空！";
                this.tb_server.Focus();
            }

            if (this.tb_email.Text.Equals(""))
            {
                check_input = false;
                check_msg = "发件邮箱地址不能为空！";
                this.tb_email.Focus();
            }

            if (this.tb_user.Text.Equals(""))
            {
                check_input = false;
                check_msg = "发件用户名地址不能为空！";
                this.tb_user.Focus();
            }

            if (this.tb_pass.Text.Equals(""))
            {
                check_input = false;
                check_msg = "发件用户密码地址不能为空！";
                this.tb_pass.Focus();
            }

            if (this.tb_email_title.Text.Equals(""))
            {
                check_input = false;
                check_msg = "邮件标题不能为空！";
                this.tb_email_title.Focus();
            }

            if (this.tb_email_body.Text.Equals(""))
            {
                check_input = false;
                check_msg = "邮件模板不能为空！";
                this.tb_email_body.Focus();
            }

            if (!check_input)
            {
                MessageBox.Show(check_msg, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            #endregion

            Fks.DataBase.SqlObject sqlobject = new Fks.DataBase.SqlObject();
            string SQL_Select = "select * from param";
            DataSet myds = sqlobject.GetDataSet(SQL_Select);
            bool result = false;

            if (myds != null && myds.Tables.Count > 0 && myds.Tables[0] != null && myds.Tables[0].Rows.Count > 0)
            {
                string SQL_Update = "update param set email_server='{0}',email_user='{1}',email_pwd='{2}',enable_ssl='{3}',send_email='{4}',email_body='{5}',split='{6}',email_title='{7}',server_type='{8}',cc_email='{9}'";
                SQL_Update = string.Format(SQL_Update, this.tb_server.Text, this.tb_user.Text, this.tb_pass.Text, this.che_Enable_SSL.Checked ? 1 : 0, this.tb_email.Text, this.tb_email_body.Text, this.cb_split.Text, this.tb_email_title.Text, this.cb_service_type.Text,this.tb_cc_email.Text);
                if (sqlobject.Exec(SQL_Update) > 0)
                    result = true;
            }
            else
            {
                string SQL_Insert = "insert into param (email_server,email_user,email_pwd,enable_ssl,send_email,email_body,split,email_title,server_type,cc_email) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')";
                SQL_Insert = string.Format(SQL_Insert, this.tb_server.Text, this.tb_user.Text, this.tb_pass.Text, this.che_Enable_SSL.Checked ? 1 : 0, this.tb_email.Text, this.tb_email_body.Text, this.cb_split.Text, this.tb_email_title.Text, this.cb_service_type.Text, this.tb_cc_email.Text);
                if (sqlobject.Exec(SQL_Insert) > 0)
                    result = true;
            }

            if (result)
            {
                var result_alert = MessageBox.Show("成功设置资料！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (result_alert == System.Windows.Forms.DialogResult.OK)
                {
                    this.Hide();
                }
            }
            else
                MessageBox.Show("系统错误！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
